<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    if (!function_exists('findUniqueMakeCount')) {

        /**
         * Create unique facilities atributes with id, title, count
         * 
         * @param   array   $items  The array of objects
         * 
         * @return  array
         */
        function findUniqueMakeCount($items) {
            // Kreiramo niz sa jednistvenim atributima
            $unique = array();

            foreach ($items as $item) // idemo kroz objekte
                if (!empty($item->facilities))
                    foreach ($item->facilities as $attributes) // idemo kroz grupe, ovdje su atributi kao std objekat
                        if (isset($attributes) && count($attributes) > 0)
                            foreach ($attributes as $attribute => $value): // idemo kroz atribute

                                if (isset($unique[$attribute])) {
                                    $unique[$attribute]->cnt++;
                                } else {
                                    $filterSTD = new stdClass;
                                    $filterSTD->id = $attribute;
                                    $filterSTD->title = $value;
                                    $filterSTD->cnt = 1;
                                    $unique[$attribute] = $filterSTD;
                                }

                            endforeach;

            return $unique;
        }

    }

    if (!function_exists('findUniquePricesMakeCount')) {


        /**
         * Create unique prices for filter with id, title, count
         * 
         * @param   array   $items  The array of objects
         * 
         * @return  array
         */
        function findUniquePricesMakeCount($items, $preview = FALSE) {
            // Kreiramo niz sa jednistvenim atributima
            $unique_price = array();
            $range[0] = array('id' => '0,50', 'title' => '€&nbsp;0 - €&nbsp;49');
            $range[1] = array('id' => '50,100', 'title' => '€&nbsp;50 - €&nbsp;99');
            $range[2] = array('id' => '100,150', 'title' => '€&nbsp;100 - €&nbsp;149');
            $range[3] = array('id' => '150,200', 'title' => '€&nbsp;150 - €&nbsp;199');
            $range[4] = array('id' => '200,-1', 'title' => '€&nbsp;200 +');

            foreach ($items as $item):                // idemo kroz objekte

                foreach ($range as $rng):                // idemo kroz price range

                    $prc_range = explode(',', $rng['id']);

                    if ($prc_range[1] == '-1')
                        $prc_range[1] = 999999;

                    if (!$preview):

                        foreach ($item->rooms as $room):                // idemo kroz sobe

                            $price = (int) $room->selling_price;

                            if (($price >= $prc_range[0]) && ($price < $prc_range[1])) {

                                if (isset($unique_price[$rng['id']])) {
                                    $unique_price[$rng['id']]->cnt++;
                                } else {
                                    $filterSTD = new stdClass;
                                    $filterSTD->id = $rng['id'];
                                    $filterSTD->title = $rng['title'];
                                    $filterSTD->cnt = 1;
                                    $unique_price[$rng['id']] = $filterSTD;
                                }

                                break 1;  /* Exit only the rooms. */
                            }

                        endforeach;


                    else:

                        $price = (int) $item->lowest_price;

                        if (($price >= $prc_range[0]) && ($price < $prc_range[1])) {

                            if (isset($unique_price[$rng['id']])) {
                                $unique_price[$rng['id']]->cnt++;
                            } else {
                                $filterSTD = new stdClass;
                                $filterSTD->id = $rng['id'];
                                $filterSTD->title = $rng['title'];
                                $filterSTD->cnt = 1;
                                $unique_price[$rng['id']] = $filterSTD;
                            }

                            break 1;  /* Exit only the rooms. */
                        }

                    endif;

                endforeach;

            endforeach;

            return $unique_price;
        }

    }

    if (!function_exists('loadJoinedGroupsAttributes')) {

        function loadJoinedGroupsAttributes(&$items) {
            $cnt = count($items);

            for ($i = 0; $i < $cnt; $i++):
                if (!empty($items[$i]->facilities))
                    foreach ($items[$i]->facilities as $attributes):// idemo kroz grupe, ovdje su atributi kao std objekat

                        if (isset($attributes) && count($attributes) > 0):

                            if (!isset($items[$i]->all_attributes))
                                $items[$i]->all_attributes = new stdClass;

                            foreach ($attributes as $attribute => $value): // idemo kroz atribute

                                $filterSTD = new stdClass;
                                $filterSTD->id = $attribute;
                                $filterSTD->title = $value;

                                $items[$i]->all_attributes->{$attribute} = $value;
                            //$items[$i]->all_attributes[$attribute] = $value;
                            endforeach;
                        endif;
                    endforeach;

            endfor;
        }

    }

    if (!function_exists('findUniqueStarsMakeCount')) {

        function findUniqueStarsMakeCount($items) {
            // Kreiramo niz sa jednistvenim brojem zvjezida od 0-5, 0 je unrated
            $unique_star = array();

            foreach ($items as $item):                // idemo kroz objekte

                $rate = "";

                if (isset($item->object_number_of_stars) && $item->object_number_of_stars != "")
                    $rate = $item->object_number_of_stars;
                else
                    $rate = 0;

                if (!isset($unique_star[$rate]))
                    $unique_star[$rate] = 0;

                $unique_star[$rate]++;



            endforeach;

            return $unique_star;
        }

    }

    if (!function_exists('filter_acc_type')) {

        /**
         * Uklanja tipove objekat koji nisu u GET nizu
         * 
         * @param   array   &$items  The array of objects
         * 
         * @return  byRef
         */
        function filter_acc_type(&$items, $accType) {

            $cnt = count($items);
            $reindex = FALSE;

            for ($index = 0; $index < $cnt; $index++) {
                fix_apartment_to_appartment($items[$index]->object_type_type);

                if ($items[$index]->object_type_type != $accType):

                    unset($items[$index]);
                    $reindex = TRUE;

                endif;
            }

            if ($reindex && count($items) > 0)
                $items = array_values($items);
        }

        function fix_apartment_to_appartment(&$type) {
            if ($type == 'apartment')
                $type = 'appartment';

            if ($type == 'villa')
                $type = 'appartment';
        }

    }

    function make_sort_price_available($items) {

        $cnt = count($items);
        for ($i = 0; $i < $cnt; $i++):

            $items[$i]->sort_price = $items[$i]->rooms[0]->selling_price;

        endfor;
        
        return $items;
    }